package com.jinzhi.common.filter;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jinzhi.common.config.InvoiceConfig;
import com.jinzhi.common.service.DictService;
import com.jinzhi.common.utils.ApiResult;
import com.jinzhi.common.utils.yongyou.ParameterUtil;
import com.jinzhi.jzweb.domain.InvoiceDO;
import com.jinzhi.jzweb.service.InvoiceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @program: InvoiceUractiveFilter
 * @description:
 * @author: Kl
 * @create: 2019-12-6 16:39
 **/
@Slf4j
@Component
public class InvoiceUractiveFilter implements Command {

    @Autowired
    private DictService dictService;
    @Autowired
    private InvoiceService invoiceService;

    @Override
    public boolean execute(Context context) throws Exception {
        String buildFpqqlsh = UUID.randomUUID().toString().replace("-", "").substring(0, 20);
        //获取返回对象
        ApiResult apiResult = (ApiResult) context.get(InvoiceConfig.API_RESULT);
        //获取返回订单信息
        Long id = (Long) context.get("OrderDO");
//        OrderDO order = orderService.selectById(id);
        EntityWrapper<InvoiceDO> wrapper = new EntityWrapper<>();
//        wrapper.eq("uid", order.getUid());
        wrapper.eq("defaultIs", true);
        //todo 这里需要获取发票信息实体
        try {
            List<InvoiceDO> invoiceDO = invoiceService.selectList(wrapper);
            if (invoiceDO != null && invoiceDO.size() > 0) {
                Map<String, String> invoiceMap = new HashMap<>();
                Map<String, String> data = new HashMap<>();
                Map<String, String> dataInfo = new HashMap<String, String>();

                ParameterUtil parameterUtil = new ParameterUtil(buildFpqqlsh);
                invoiceMap.put("url", parameterUtil.buildUrlConfigs());//用友发票回调路径
                //上送参数email
                invoiceMap.put("email", parameterUtil.buildEmailConfigs("lance.kang@qq.com"));//发票邮件信息

                //获取数据字典配置的税率
                Map<String, String> map = new HashMap<>();
                map.put("name", "税率");
                map.put("type", "tax_rate");
                String dictDO = dictService.selDicBy(map);//todo 这里需要在数据字典配置
                if (null == dictDO) {
                    throw new Exception("请先配置税率");
                }
                //保存发票明细信息
                List<Object> list = new ArrayList<>();
                data.put("XMJSHJ", String.valueOf(111));//项目价税合计
                data.put("XMMC", "华豪测试");//项目名称
                data.put("SL", dictDO);//税率
                data.put("SPBM", "304050202990200000");//商品编码
                data.put("XMSL", "1");//项目数量
                list.add(data);

                //保存发票信息
                dataInfo.put("XMMC", "华豪测试");
                dataInfo.put("SL", dictDO);//税率
//                dataInfo.put("bigMoney", String.valueOf(order.getPrices()));
                dataInfo.put("invoiceId", new Long(invoiceDO.get(0).getId()).toString());
                dataInfo.put("buildFpqqlsh", buildFpqqlsh);
//                dataInfo.put("buildList", String.valueOf(order.getId()));
                dataInfo.put("type", "订单开具发票");

                invoiceMap.put("requestdatas", parameterUtil.buildRequestDatas(
                        invoiceDO.get(0).getInvoiceRise(), invoiceDO.get(0).getDutyParagraph(), 111, list));
                invoiceMap.put("autoAudit", "true");
                context.put(InvoiceConfig.INVOICE_MAP, invoiceMap);
                context.put("DATAINFO", dataInfo);
            } else {
                apiResult.setCode(InvoiceConfig.ERROR_CODE);
                apiResult.setMsg("该用户没有创建发票信息,请先添加发票");
                return true;
            }
        } catch (Exception e) {
            apiResult.setCode(InvoiceConfig.ERROR_CODE);
            apiResult.setMsg("组装Map失败");
            return true;
        }
        return false;
    }

}
